/**
 * 标签类
 * @namespace Note.Widget.Label
 *
 * 控件接口,每个控件必须实现
 * interface Widget{
 * 		String name;		 // 控件类名
 * 		HTMLElement element; // html元素对象
 * 		void load(data);     // 通过data数据还原对象
 * 		object save();	  	 // 保存对象
 * 		void clear();	  	 // 清空控件数据
 * }
 * 
 * 构造函数
 * @constructor Label
 * @author JunEnstein 2017-05-20
 * @param  {Point{x:int, y:int}} position 位置
 */
Note.Widget.Label = function(position){
	/**
	 * 类名
	 * @type {String}
	 */
	this._name = "Label";
	/**
	 * HTML元素对象
	 * @type {HTMLElement}
	 */
	this._element = null;
	this._toggleDiv = null;
	this._toggleBtn = null;
	this._labelDiv = null;
	this._textDiv = null;

	/**
	 * label对象id
	 * @type {Number}
	 */
	this._id = null;
	/**
	 * Label对象内容
	 * @type {String}
	 */
	this._text = null;

	/**
	 * 标签开启状态
	 * @type {Boolean}
	 */
	this._isOpen = true;

	/**
	 * 创建元素对象
	 */
	this._createElement(position);

	/**
	 * 删除事件(为了应对,父类共享一块内存的,临时解决方案)
	 * @type {Array}
	 */
	this.ondelete = new Array();

	/**
	 * 拖拽事件处理
	 */
	$(this._element).drag(this._toggleBtn);

	/**
	 * 增加事件处理函数
	 */
	EventOnHandler(this._toggleBtn, "click", this, "toggleLabel");
	EventOnHandler(this._deleteBtn, "click", this, "deleted");
};

/**
 * 继承事件对象(有bug, 当前所有子类对象共用一份父类对象内存)
 * @type {Note}
 */
Note.Widget.Label.prototype = new Note.Event();

/**
 * 创建Label对象的HTML元素
 * @method _createElement
 * @param  {Point{x:int, y:int}} position 位置
 */
Note.Widget.Label.prototype._createElement = function(position){
	/**
	 * CSS样式类名称
	 * @type {Object}
	 */
	var classNames = {
		element: "note-label",
		toggleDiv: "toggleDiv",
		toggleButton: "toggleButton",
		labelDiv: "labelDiv",
		textDiv: "textDiv",
		deleteButton: "deleteButton"
	};

	/**
	 * 创建HTML元素对象
	 */
    this._element = document.createElement("div");
	this._element.style.left = position.x + 'px';
	this._element.style.top  = position.y + 'px';
	this._element.className = classNames['element'];
	
	this._toggleDiv = document.createElement("div");
	this._toggleDiv.className = classNames.toggleDiv;
	
	this._toggleBtn = document.createElement("div");
	this._toggleBtn.className = classNames.toggleButton;

	this._labelDiv = document.createElement("div");
	this._labelDiv.className = classNames.labelDiv;
	
	this._textDiv = document.createElement("div");
	this._textDiv.className = classNames.textDiv;
	this._textDiv.contentEditable = true;
	
	this._deleteBtn = document.createElement("div");
	this._deleteBtn.className = classNames.deleteButton;
	
	this._toggleDiv.appendChild(this._toggleBtn);

    this._labelDiv.appendChild(this._textDiv);
	this._labelDiv.appendChild(this._deleteBtn);

	this._element.appendChild(this._toggleDiv);
	this._element.appendChild(this._labelDiv);
};

/**
 * 切换label的显示或者隐藏状态
 * @method toggleLabel
 */
Note.Widget.Label.prototype.toggleLabel = function(){
	if(this._labelDiv.style.display == "block"){	
        this._labelDiv.style.display = "none";
	}
	else{
        this._labelDiv.style.display = "block";
    }
};

/**
 * 设置Label对象id
 * @method setId
 * @param  {Number} id id值
 */
Note.Widget.Label.prototype.setId = function(id){
	if(id == null) return ;
    this._id = id;
	this._toggleBtn.innerHTML = id;
};

/**
 *	函数名: setText
 *	函数功能: 设置Label内容
 *	@param text {String} label的内容
 */
Note.Widget.Label.prototype.setText = function(text){
	var contents = text;
	if(contents == null) contents = "";
	this._text = contents;
	this._textDiv.innerHTML = contents;
};    

/**
 * 通过data还原对象内容
 * @method load
 * @author JunEnstein 2017-05-21
 * @param  {Object} data 需要还原的数据对象
 */
Note.Widget.Label.prototype.load = function(data){
	this.setId(data.id);
	this.setText(data.text);
	if(data.isOpen != null) this._isOpen = data.isOpen;
	if(this._isOpen){
		this._labelDiv.style.display = "block";
	}else{
		this._labelDiv.style.display = "none";
	}
};

/**
 * 保存Label对象的内容
 * @method save
 * @author JunEnstein 2017-05-21
 * @return {Object} Label对象的内容
 */
Note.Widget.Label.prototype.save = function(){
	this.setText(this._textDiv.innerHTML);
	var data = {
		name : this._name,
		isOpen: this._isOpen,
		id : this._id,
		text: this._text,
		position: {
			x : parseInt(this._element.style.left),
			y : parseInt(this._element.style.top),
		}
	};
	return data;
};

Note.Widget.Label.prototype.clear = function(){};